/*    */ package com.newcrm.util;
/*    */ 
/*    */ import com.newcrm.exception.MailToIsNullException;
/*    */ import java.io.File;
/*    */ import java.io.UnsupportedEncodingException;
/*    */ import java.util.ArrayList;
/*    */ import java.util.List;
/*    */ import javax.mail.internet.MimeUtility;
/*    */ import org.apache.commons.mail.DefaultAuthenticator;
/*    */ import org.apache.commons.mail.Email;
/*    */ import org.apache.commons.mail.EmailAttachment;
/*    */ import org.apache.commons.mail.EmailException;
/*    */ import org.apache.commons.mail.HtmlEmail;
/*    */ import org.apache.commons.mail.SimpleEmail;
/*    */ 
/*    */ public class EmailUtil
/*    */ {
/*    */   public static void checkEmailSetting(String host, int port, String username, String password, boolean isSSL, String emailAddress)
/*    */     throws EmailException
/*    */   {
/* 24 */     Email email = new SimpleEmail();
/* 25 */     email.setCharset("UTF-8");
/* 26 */     email.setHostName(host);
/* 27 */     email.setSmtpPort(port);
/* 28 */     email.setAuthenticator(new DefaultAuthenticator(username, password));
/* 29 */     email.setSSLOnConnect(isSSL);
/* 30 */     email.setFrom(emailAddress);
/* 31 */     email.setSubject("验证邮箱的SMTP设置");
/* 32 */     email.setMsg("这是一封给自己发送的邮件，用以验证SMTP信息是否设置正确");
/* 33 */     email.addTo(emailAddress);
/* 34 */     email.send();
/*    */   }
/*    */ 
/*    */   public static void sendEmail(String host, int port, String username, String password, boolean isSSL, String from, String to, String subject, String content, String attacheFiles, String baseFilePath, String loginUserName)
/*    */     throws EmailException, MailToIsNullException
/*    */   {
/* 42 */     if ((to == null) || (to.trim().length() == 0)) {
/* 43 */       throw new MailToIsNullException();
/*    */     }
/*    */ 
/* 46 */     HtmlEmail email = new HtmlEmail();
/* 47 */     email.setCharset("UTF-8");
/* 48 */     email.setHostName(host);
/* 49 */     email.setSmtpPort(port);
/* 50 */     email.setAuthenticator(new DefaultAuthenticator(username, password));
/* 51 */     email.setSSLOnConnect(isSSL);
/* 52 */     email.setFrom(from, loginUserName);
/* 53 */     email.setSubject(subject);
/* 54 */     email.setHtmlMsg("<html><body>" + content + "</body></html>");
/*    */     int idx1;
/* 57 */     for (String add : to.split(";"))
/*    */     {
/* 59 */       idx1 = add.indexOf('<');
/* 60 */       int idx2 = add.indexOf('>');
/* 61 */       if ((idx1 > 0) && (idx2 > 0)) {
/* 62 */         String toName = add.substring(0, idx1).trim();
/* 63 */         String toEmail = add.substring(idx1 + 1, idx2).trim();
/* 64 */         email.addTo(toEmail, toName, "utf-8");
/*    */       } else {
/* 66 */         email.addTo(add);
/*    */       }
/*    */     }
/*    */ 
/* 70 */     List deleteFiles = new ArrayList();
/* 71 */     if ((attacheFiles != null) && (attacheFiles.trim().length() > 0))
/* 72 */       for (String attachFileInfo : attacheFiles.split(",")) {
/* 73 */         String[] tmp = attachFileInfo.split(";");
/* 74 */         if ((tmp != null) && (tmp.length == 2))
/*    */         {
/* 78 */           String saveFileName = tmp[0];
/*    */ 
/* 80 */           String fileName = tmp[1];
/*    */ 
/* 82 */           EmailAttachment e = new EmailAttachment();
/*    */           try
/*    */           {
/* 85 */             e.setName(MimeUtility.encodeText(fileName));
/*    */           } catch (UnsupportedEncodingException e1) {
/* 87 */             e.setName(fileName);
/*    */           }
/* 89 */           String filePath = baseFilePath + File.separator + saveFileName;
/* 90 */           deleteFiles.add(filePath);
/* 91 */           e.setPath(filePath);
/* 92 */           e.setDisposition("attachment");
/* 93 */           email.attach(e);
/*    */         }
/*    */       }
/* 96 */     email.send();
/*    */ 
/* 99 */     for (Object f : deleteFiles) {
/* 100 */       File t = new File(f.toString());
/*    */       try {
/* 102 */         t.delete();
/*    */       }
/*    */       catch (SecurityException localSecurityException)
/*    */       {
/*    */       }
/*    */     }
/*    */   }
/*    */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.util.EmailUtil
 * JD-Core Version:    0.6.1
 */